package com.lrkj.waitress.service.impl;

import java.util.List;
import com.lrkj.common.utils.DateUtils;
import com.lrkj.common.utils.SecurityUtils;
import com.lrkj.common.utils.uuid.IdUtils;
import com.lrkj.property.domain.Community;
import com.lrkj.property.service.ICommunityService;
import com.lrkj.waitress.domain.CruiserTaskDetail;
import com.lrkj.waitress.mapper.CruiserTaskDetailMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lrkj.waitress.mapper.CruiserTaskMapper;
import com.lrkj.waitress.domain.CruiserTask;
import com.lrkj.waitress.service.ICruiserTaskService;

/**
 * 巡逻任务Service业务层处理
 *
 * @author yajun_ren
 * @date 2022-03-23
 */
@Service
public class CruiserTaskServiceImpl implements ICruiserTaskService
{
    @Autowired
    private CruiserTaskMapper cruiserTaskMapper;

    @Autowired
    private CruiserTaskDetailMapper cruiserTaskDetailMapper;


    @Autowired
    private ICommunityService communityService;

    /**
     * 查询巡逻任务
     *
     * @param id 巡逻任务ID
     * @return 巡逻任务
     */
    @Override
    public CruiserTask selectCruiserTaskById(String id)
    {
        return cruiserTaskMapper.selectCruiserTaskById(id);
    }

    /**
     * 查询巡逻任务列表
     *
     * @param cruiserTask 巡逻任务
     * @return 巡逻任务
     */
    @Override
    public List<CruiserTask> selectCruiserTaskList(CruiserTask cruiserTask)
    {
        List<CruiserTask> taskList = cruiserTaskMapper.selectCruiserTaskList(cruiserTask);
        if (null != taskList && taskList.size()>0) {
            for (int i = 0; i < taskList.size(); i++) {
                if (taskList.get(i).getStatecount().equals("0")) {
                    taskList.set(i,taskList.get(i)).setTaskState("20");
                    cruiserTaskMapper.updateCruiserTask(taskList.get(i));
                }
            }
        }
        return taskList;
    }

    /**
     * 新增巡逻任务
     *
     * @param cruiserTask 巡逻任务
     * @return 结果
     */
    @Override
    public int insertCruiserTask(CruiserTask cruiserTask)
    {
        cruiserTask.setId(IdUtils.simpleUUID());
        cruiserTask.setCreateBy(SecurityUtils.getUsername());
        cruiserTask.setCreateTime(DateUtils.getNowDate());
        this.setInfo(cruiserTask);
        CruiserTaskDetail cruiserTaskDetail = new CruiserTaskDetail();
        List<String> pathDetailList = cruiserTask.getPathDetailList();
//        for (int i = 0; i < pathDetailList.size(); i++) {
//            cruiserTaskDetail.setId(IdUtils.simpleUUID());
//            cruiserTaskDetail.setPlaceId(pathDetailList.get(i));
//            cruiserTaskDetail.setTaskId(cruiserTask.getId());
//            cruiserTaskDetail.setState("10");
//            cruiserTaskDetail.setCreateBy(SecurityUtils.getUsername());
//            cruiserTaskDetail.setCreateTime(DateUtils.getNowDate());
//            cruiserTaskDetailMapper.insertCruiserTaskDetail(cruiserTaskDetail);
//        }
        return cruiserTaskMapper.insertCruiserTask(cruiserTask);
    }

    /**
     * 修改巡逻任务
     *
     * @param cruiserTask 巡逻任务
     * @return 结果
     */
    @Override
    public int updateCruiserTask(CruiserTask cruiserTask)
    {
        cruiserTask.setUpdateBy(SecurityUtils.getUsername());
        cruiserTask.setUpdateTime(DateUtils.getNowDate());
        this.setInfo(cruiserTask);
        cruiserTaskDetailMapper.deleteCruiserTaskDetailById(cruiserTask.getId());
//        CruiserTaskDetail cruiserTaskDetail = new CruiserTaskDetail();
//        List<String> pathDetailList = cruiserTask.getPathDetailList();
//        for (int i = 0; i < pathDetailList.size(); i++) {
//            cruiserTaskDetail.setId(IdUtils.simpleUUID());
//            cruiserTaskDetail.setPlaceId(pathDetailList.get(i));
//            cruiserTaskDetail.setTaskId(cruiserTask.getId());
//            cruiserTaskDetail.setState("10");
//            cruiserTaskDetail.setCreateBy(SecurityUtils.getUsername());
//            cruiserTaskDetail.setCreateTime(DateUtils.getNowDate());
//            cruiserTaskDetailMapper.insertCruiserTaskDetail(cruiserTaskDetail);
//        }
        return cruiserTaskMapper.updateCruiserTask(cruiserTask);
    }

    /**
     * 批量删除巡逻任务
     *
     * @param ids 需要删除的巡逻任务ID
     * @return 结果
     */
    @Override
    public int deleteCruiserTaskByIds(String[] ids)
    {
        cruiserTaskDetailMapper.deleteCruiserTaskDetailByIds(ids);
        return cruiserTaskMapper.deleteCruiserTaskByIds(ids);
    }

    /**
     * 删除巡逻任务信息
     *
     * @param id 巡逻任务ID
     * @return 结果
     */
    @Override
    public int deleteCruiserTaskById(String id)
    {
        cruiserTaskDetailMapper.deleteCruiserTaskDetailById(id);
        return cruiserTaskMapper.deleteCruiserTaskById(id);
    }

    @Override
    public int updCruiserOff(CruiserTask cruiserTask) {
        cruiserTask.setTaskState("20");
        return cruiserTaskMapper.updateCruiserTask(cruiserTask);
    }

    /**
     * 批量新增巡逻任务信息
     *
     * @param cruiserTasks 巡逻任务对象
     */
    public int batchAddData(List<CruiserTask> cruiserTasks)
    {
           return cruiserTaskMapper.batchAddData(cruiserTasks);
    }


    private void setInfo(CruiserTask  cruiserTask) {
        Community community = communityService.selectCommunityById(cruiserTask.getCommunityId());
        cruiserTask.setDeptId(community.getDeptId());
        cruiserTask.setAncestors(community.getAncestors() + "," + community.getId());
    }
}
